package service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import model.Tgood;
import model.Tinventory;
import model.Treceipt;
import model.Tshoplist;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.GoodDaoI;
import dao.InventoryDaoI;
import dao.ReceiptDaoI;
import dao.ShopListDaoI;
import pageModel.DataGrid;
import pageModel.ProductStatistic;
import pageModel.Receipt;
import pageModel.SaleHighchart;
import pageModel.ShopList;
import service.ShopListServiceI;
@Service("shopListService")
public class ShopListServiceImpl implements ShopListServiceI {
private ShopListDaoI shopListDao;
private GoodDaoI goodDao;
private ReceiptDaoI receiptDao;
private InventoryDaoI inventoryDao;
	public ShopListDaoI getShopListDao() {
	return shopListDao;
}
@Autowired
public void setShopListDao(ShopListDaoI shopListDao) {
	this.shopListDao = shopListDao;
}

public GoodDaoI getGoodDao() {
	return goodDao;
}
@Autowired
public void setGoodDao(GoodDaoI goodDao) {
	this.goodDao = goodDao;
}

	public ReceiptDaoI getReceiptDao() {
	return receiptDao;
}
	@Autowired
public void setReceiptDao(ReceiptDaoI receiptDao) {
	this.receiptDao = receiptDao;
}
	public InventoryDaoI getInventoryDao() {
		return inventoryDao;
	}
	@Autowired
	public void setInventoryDao(InventoryDaoI inventoryDao) {
		this.inventoryDao = inventoryDao;
	}
	@Override
	public void saveOrUpdate(String tid,String codes,String amounts,String nprices) {
		String [] ncodes=codes.split(",");
		String[] namounts=amounts.split(",");
		String[] nnprices=nprices.split(",");
		for(int i=0;i<ncodes.length;i++){
			String hql = "from Tshoplist t where t.treceipt.tid=:tid and t.tgood.code=:code";
			Map<String,Object>params=new HashMap<String,Object>();
			params.put("tid",tid);
			params.put("code",ncodes[i].toString());
			Tshoplist os = shopListDao.get(hql, params);
			if(os!=null){
					os.setAmount(Integer.parseInt((namounts[i])));
					os.setPrice(Double.parseDouble((nnprices[i])));
				    shopListDao.save(os);
			}else{
			  Tshoplist s=new Tshoplist();
			  String hql1="from Tgood t where t.code=:code";
			  Map<String,Object> params1=new HashMap<String,Object>();
			  params1.put("code" , ncodes[i].toString());
			  Tgood tg=goodDao.get(hql1,params1);
			  String hql2="from Treceipt t where t.tid=:tid";
			  Map<String,Object> params2=new HashMap<String,Object>();
			  params2.put("tid" , tid);
			  Treceipt r=receiptDao.get(hql2,params2);
			  s.setId(UUID.randomUUID().toString());
			  s.setTreceipt(r);
			  s.setTgood(tg);
			  s.setAmount(Integer.parseInt((namounts[i])));
			  s.setPrice(Double.parseDouble((nnprices[i])));
			  shopListDao.save(s);
			}
		}
	}
	@Override
	public DataGrid show(ShopList shopList) {
		DataGrid dg=new DataGrid();
		String hql="from Tshoplist t where t.treceipt.tid=:tid";
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("tid", shopList.getTid());
		String totalHql="select count(*) "+hql;
		if(shopList.getSort()!=null){
		hql+=" order by "+shopList.getSort()+" "+shopList.getOrder();
		}
		List<Tshoplist> ts=shopListDao.find(hql,params,shopList.getPage(),shopList.getRows());
		List<ShopList> l=new ArrayList<ShopList>();
		if(ts!=null&&ts.size()>0){
			for(Tshoplist t :ts){
				ShopList s = new ShopList();
				BeanUtils.copyProperties(t, s);
				s.setTid(t.getTreceipt().getTid());
				if(t.getTgood()!=null){
					s.setCode(t.getTgood().getCode());
					s.setName(t.getTgood().getName());
					s.setUnit(t.getTgood().getUnit());
					s.setPrices(t.getTgood().getPrice());
				}
				if(t.getTreceipt().getTvip()!=null){
					s.setVip(t.getTreceipt().getTvip().getId());
					s.setDiscount(t.getTreceipt().getTvip().getDiscount());
				}else{
					s.setDiscount(1.0);
				}
				l.add(s);
			}
		}
		dg.setTotal(shopListDao.count(totalHql,params));
		dg.setRows(l);
		return dg;
	}
	@Override
	public void updateInventory(String codes, String amounts) {
		String [] ncodes=codes.split(",");
		String[] namounts=amounts.split(",");
		for(int i=0;i<ncodes.length;i++){
		String hql = "from Tinventory t where t.tgood.code=:code";
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("code",ncodes[i].toString());
		Tinventory oi = inventoryDao.get(hql, params);
		oi.setAmount(oi.getAmount()-Integer.parseInt((namounts[i])));
		inventoryDao.save(oi);
		}
	}
	@Override
	public DataGrid outputReceipt(ShopList shopList) {
		DataGrid dg = new DataGrid();
		String hql="from Tshoplist t where t.treceipt.tid=:tid and t.amount>0";
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("tid",shopList.getTid());
		String totalHql="select count(*) "+hql;
		List<Tshoplist> ts=shopListDao.find(hql,params,shopList.getPage(),shopList.getRows());
		List<ShopList> l=new ArrayList<ShopList>();
		String hql1="from Treceipt t where t.tid=:tid";
		Map<String,Object> params1=new HashMap<String,Object>();
		params1.put("tid",shopList.getTid());
		Treceipt tr=receiptDao.get(hql1,params1);
		if(ts!=null&&ts.size()>0){
			for(Tshoplist t :ts){
				ShopList s = new ShopList();
				BeanUtils.copyProperties(t, s);
				if(t.getTgood()!=null){
					s.setCode(t.getTgood().getCode());
					s.setName(t.getTgood().getName());
					s.setUnit(t.getTgood().getUnit());
					s.setPrices(t.getTgood().getPrice());
				}
				if(tr.getTvip()!=null){
					s.setVip(tr.getTvip().getCardid());
					s.setDiscount(tr.getTvip().getDiscount());
					s.setIntegration(tr.getTvip().getIntegration());
				}
				l.add(s);
			}
		 }
		dg.setTotal(shopListDao.count(totalHql,params));
		dg.setRows(l);
		return dg;
	}
	@Override
	public List<ProductStatistic> jasper(ShopList shopList) {
		String hql="select new Map(year(t.treceipt.date) as year,month(t.treceipt.date) as month, t.tgood.tgoodtypes.name as style,t.tgood.unit as unit,sum(t.amount) as amount,sum(t.price) as total) from Tshoplist t where 1=1";
		Map<String,Object> params=new HashMap<String,Object>();
		if (shopList.getCreatedatetimeStart() != null) {
			hql += " and t.treceipt.date >= :createdatetimeStart";
			params.put("createdatetimeStart", shopList.getCreatedatetimeStart());
		}
		if (shopList.getCreatedatetimeEnd() != null) {
			hql += " and t.treceipt.date <= :createdatetimeEnd";
			params.put("createdatetimeEnd", shopList.getCreatedatetimeEnd());
		}
		if (shopList.getCreatedatetimeStart() == null&&shopList.getCreatedatetimeEnd() == null) {
			Calendar now = Calendar.getInstance(); 
			int year =  now.get(Calendar.YEAR);
			hql += " and year(t.treceipt.date)= :yeartime";
			params.put("yeartime", year);
		}
		String totalHql=hql + " group by t.tgood.tgoodtypes.name,month(t.treceipt.date)";
		List<Map<String, Object>> l = shopListDao.findByMap(totalHql,params);
		List<ProductStatistic> ls = new ArrayList<>();
		for(Map<String, Object> map : l){
			ProductStatistic p = new ProductStatistic();
			p.setYear((Integer)map.get("year"));
			p.setMonth((Integer)map.get("month"));
			p.setStyle((String)map.get("style"));
			p.setAmount((Long)map.get("amount"));
			p.setUnit((String)map.get("unit"));
			p.setTotal((Double)map.get("total"));
			ls.add(p);
		}
		return ls;
	}
	@Override
	public List<SaleHighchart> highChart(Receipt receipt) {
		List<SaleHighchart> ls = new ArrayList<>();
		String hql1="select distinct new Map(t.tgood.tgoodtypes.name as style) from Tshoplist t where 1=1";
		Map<String,Object> params1=new HashMap<String,Object>();
		if (receipt.getCreatedatetimeStart() != null) {
			hql1 += " and t.treceipt.date >= :createdatetimeStart";
			params1.put("createdatetimeStart", receipt.getCreatedatetimeStart());
		}
		if (receipt.getCreatedatetimeEnd() != null) {
			hql1 += " and t.treceipt.date <= :createdatetimeEnd";
			params1.put("createdatetimeEnd", receipt.getCreatedatetimeEnd());
		}
		if (receipt.getCreatedatetimeStart() == null&&receipt.getCreatedatetimeEnd() == null) {
			Calendar now = Calendar.getInstance(); 
			int year =  now.get(Calendar.YEAR);
			hql1 += " and year(t.treceipt.date)= :yeartime";
			params1.put("yeartime", year);
		}
		List<Map<String, Object>> list = shopListDao.findByMap(hql1,params1);
		for(Map<String, Object> map : list){
			SaleHighchart s = new SaleHighchart();
			List<Object> listdata=new ArrayList<>();
			String hql="select new Map(year(t.treceipt.date) as year,month(t.treceipt.date) as month, t.tgood.tgoodtypes.name as style,t.tgood.unit as unit,sum(t.amount) as amount,sum(t.price) as total) from Tshoplist t where t.tgood.tgoodtypes.name=:style";
			Map<String,Object> params=new HashMap<String,Object>();
			params.put("style", (String)map.get("style"));
			String totalHql=hql + " group by month(t.treceipt.date)";
			List<Map<String, Object>> l = shopListDao.findByMap(totalHql,params);
			for(Map<String, Object> maps : l){
				s.setName((String)maps.get("style"));
				List<Object> data=new ArrayList<>();
				data.add((Integer)maps.get("month"));
				data.add((Long)maps.get("amount"));
				listdata.add(data);
			}
			s.setData(listdata);
			ls.add(s);
		}
		return ls;
  }
	public List<SaleHighchart> saleHighChart(Receipt receipt) {
		List<SaleHighchart> ls = new ArrayList<>();
		String hql1="select distinct new Map(t.tgood.tgoodtypes.name as style) from Tshoplist t where 1=1";
		Map<String,Object> params1=new HashMap<String,Object>();
		if (receipt.getCreatedatetimeStart() != null) {
			hql1 += " and t.treceipt.date >= :createdatetimeStart";
			params1.put("createdatetimeStart", receipt.getCreatedatetimeStart());
		}
		if (receipt.getCreatedatetimeEnd() != null) {
			hql1 += " and t.treceipt.date <= :createdatetimeEnd";
			params1.put("createdatetimeEnd", receipt.getCreatedatetimeEnd());
		}
		if (receipt.getCreatedatetimeStart() == null&&receipt.getCreatedatetimeEnd() == null) {
			Calendar now = Calendar.getInstance(); 
			int year =  now.get(Calendar.YEAR);
			hql1 += " and year(t.treceipt.date)= :yeartime";
			params1.put("yeartime", year);
		}
		List<Map<String, Object>> list = shopListDao.findByMap(hql1,params1);
		for(Map<String, Object> map : list){
			SaleHighchart s = new SaleHighchart();
			List<Object> listdata=new ArrayList<>();
			String hql="select new Map(year(t.treceipt.date) as year,month(t.treceipt.date) as month, t.tgood.tgoodtypes.name as style,t.tgood.unit as unit,sum(t.amount) as amount,sum(t.price) as total) from Tshoplist t where t.tgood.tgoodtypes.name=:style";
			Map<String,Object> params=new HashMap<String,Object>();
			params.put("style", (String)map.get("style"));
			String totalHql=hql + " group by month(t.treceipt.date)";
			List<Map<String, Object>> l = shopListDao.findByMap(totalHql,params);
			for(Map<String, Object> maps : l){
				s.setName((String)maps.get("style"));
				List<Object> data=new ArrayList<>();
				data.add((Integer)maps.get("month"));
				data.add((Double)maps.get("total"));
				listdata.add(data);
			}
			s.setData(listdata);
			ls.add(s);
		}
		return ls;
   }
}